/**
 * 详情对话框
 */
var InterfaceapprovalInfoDlg = {
    data: {
        id: "",
        userId: "",
        content: "",
        department: "",
        system: "",
        servicetype: "",
        idescribe: "",
        state: "",
        remarks: "",
        revision: "",
        createdBy: "",
        createdTime: "",
        updatedBy: "",
        updatedTime: ""
    }
};

layui.use(['form', 'admin', 'ax', 'laydate', 'upload', 'formSelects', 'func', 'layer'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var func = layui.func;
    var layer = layui.layer;


    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/interfaceapproval/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();
    var data = result.data;
    console.log(data)
    if (data.state == 0) {
        data.state = "等待审核"
    } else if (data.state == 1) {
        data.state = "已审核通过"
        $("#remarks").attr('disabled', true)
    } else if (data.state == 2) {
        data.state = "已被驳回"
        $("#remarks").attr('disabled', true)
    } else if (data.state == 3) {
        data.state = "已完成"
    }
    form.val('interfaceapprovalForm', result.data);


    /**
     * 根据状态追加按钮
     */
    if (data.state == "等待审核") {
        $("#btnSubmit").empty();
        var a = '<div class="layui-col-md6">';
        a += '<button class="layui-btn layui-btn-normal" id="approve" type="button">&nbsp;批准&nbsp;</button>';
        a += '</div>';
        a += '<div class="layui-col-md6">';
        a += '<button class="layui-btn layui-btn-normal" id="reject" type="button">&nbsp;驳回&nbsp;</button>';
        a += '</div>';
        $("#btnSubmit").append(a)
    } else if (data.state == "已审核通过") {
        $("#btnSubmit").empty();
        var a = '<div class="layui-col-md6 layui-col-md-offset3">';
        a += '<button class="layui-btn layui-btn-normal" id="done" type="button">&nbsp;开发完成&nbsp;</button>';
        a += '</div>'
        $("#btnSubmit").append(a)
    } else if (data.state == "已被驳回") {
        $("#btnSubmit").empty();
    }

    /**
     * 点击批准
     */
    var ids = new Array();
    var id = Feng.getUrlParam("id");
    // console.log(id)
    ids.push(id)
    // console.log(ids)
    $("#btnSubmit").on('click', '#approve', function () {
        if ($("#remarks").val() == "") {
            Feng.error("请填写备注")
            return
        }
        // sessionStorage.setItem("id", id);
        $.ajax(Feng.ctxPath + '/interfaceapproval/approve', {
            data: {
                ids: ids
            },
            traditional: true,
            async: false,
            success: function () {
                Feng.success('批准成功')
                // parent.location.reload()
            },
            error: function () {
                Feng.error('批准失败')
            }
        })

        var ajax1 = new $ax(Feng.ctxPath + "/interfaceapproval/detail?id=" + id);
        var res = ajax1.start();


        $.ajax(Feng.ctxPath + '/interfaceapproval/editItem', {
            async: false,
            data: {
                id: res.data.id,
                userId: res.data.userId,
                interfaceId: res.data.interfaceId,
                content: res.data.content,
                serviceid: res.data.serviceid,
                department: res.data.department,
                echo: res.data.echo,
                description: res.data.description,
                system: res.data.system,
                servicetype: res.data.servicetype,
                idescribe: res.data.idescribe,
                state: res.data.state,
                remarks: $("#remarks").val(),
                revision: res.data.revision,
                createdBy: res.data.createdBy,
                createdTime: res.data.createdTime,
                updatedBy: res.data.updatedBy,
                updatedTime: res.data.updatedTime
            },
            success: function () {
            },
        })
        location.reload();
    })

    /**
     * 驳回
     */
    $("#btnSubmit").on('click', '#reject', function () {
        // sessionStorage.setItem("id", id);
        if ($("#remarks").val() == "") {
            Feng.error("请填写备注")
            return
        }
        $.ajax(Feng.ctxPath + '/interfaceapproval/reject', {
            data: {
                ids: ids
            },
            traditional: true,
            async: false,
            success: function (res) {
                // console.log(res)
                if (res == ids.length) {
                    Feng.success('驳回成功')
                    // parent.location.reload()
                }
            },
            error: function () {
                Feng.error('驳回失败')
            }
        })

        var ajax2 = new $ax(Feng.ctxPath + "/interfaceapproval/detail?id=" + id);
        var res = ajax2.start();


        $.ajax(Feng.ctxPath + '/interfaceapproval/editItem', {
            async: false,
            data: {
                id: res.data.id,
                userId: res.data.userId,
                interfaceId: res.data.interfaceId,
                content: res.data.content,
                serviceid: res.data.serviceid,
                department: res.data.department,
                echo: res.data.echo,
                description: res.data.description,
                system: res.data.system,
                servicetype: res.data.servicetype,
                idescribe: res.data.idescribe,
                state: res.data.state,
                remarks: $("#remarks").val(),
                revision: res.data.revision,
                createdBy: res.data.createdBy,
                createdTime: res.data.createdTime,
                updatedBy: res.data.updatedBy,
                updatedTime: res.data.updatedTime
            },
            success: function () {
            },
        })
        admin.closeThisDialog();
    })


    /**
     * 点击开发完成
     */
    $("#btnSubmit").on('click', '#done', function () {
        sessionStorage.setItem("data", JSON.stringify(data))
        layer.open({
            title: '',
            area: ['100%', '100%'],
            content: Feng.ctxPath + "/interfacelist/add",
            maxmin: true,
            type: 2,
            success: function (layero) {
                //移除最小化
                layero.find('.layui-layer-min').remove();
            },
            end: function () {
                admin.closeThisDialog()
            },
        })
    })

});